<meta charset="utf-8">
<link rel="stylesheet" href="common.css">
<style>
	form {
		text-align: center;
		margin-top: 240px;
	}

	.btn {
		width: 50px;
		height: 30px;
	}
</style>
<?php
//判断cookie里是否存在帐号密码 如果存在获取
$username = "";
$password = "";

if(isset($_COOKIE["username"])) {
	//获取cookie里的值
	$username = $_COOKIE["username"];
}

if(isset($_COOKIE["password"])) {
	$password = $_COOKIE["password"];
}
?>

<form method="post" action="">
	用&nbsp;户&nbsp;名:<input type="text" name="username" value="<?php echo $username;?>"><br/><br/>
	密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" value="<?php echo $password;?>"><br/><br/>
	
	<input type="submit" name="submit" value="登录" class="btn">
</form>

<?php
if(isset($_POST["submit"]) && $_POST["submit"] == "登录") {
	//是登录按钮触发的事件
	$username = $_POST["username"];
	$password = $_POST["password"];

	//查询数据库 看看是否存在匹配的用户
	$selectusersql = "select * from userinfos where username='$username' and password='$password'";
	
	include_once("database.php");

	$result = @mysql_query($selectusersql) or die("登录失败:".mysql_error()."<br/>");

	if(mysql_num_rows($result) > 0) {
		//输入的用户名和密码是正确的
		//写cookie信息
		setcookie("username",$username,time()+60*60);
		//跳转页面到主界面
		header("refresh:1;url=index.php");

	} else {

		//输入的用户名和密码不存在
	    echo "<script>alert(\"用户名或密码错误!\");</script>";
	}
}
?> 